package com.starblink.brand.adapter

import android.view.View
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.ext.visibleOrInVisible
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.sensorsdata.extension.businessSuffixId
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.basic.route.RoutePage
import com.starblink.basic.util.CheckUtil
import com.starblink.brand.bean.BrandAllBean
import com.starblink.brand.databinding.CellBrandAllBinding

class BrandAllCell(
    binding: CellBrandAllBinding,
    private val getSubState: (brandId: Any) -> Boolean?,
) :
    BaseBindingVH<BrandAllBean, CellBrandAllBinding>(binding) {
    override fun onBind(model: BrandAllBean, position: Int) {
        val brandFList = model.brandFList
        brandFList?.let {
            binding.llBrand1.visibleOrGone(visible = it.size > 0, goneMode = View.INVISIBLE) {
                val brandAlltemBean = it[0]
                val brandF = brandAlltemBean.topBrandInfoVOF
                binding.ivBrandLogo1.load(brandF.logo, brandF.name)
                binding.tvBrandName1.text = brandF.name
                binding.llDiscount1.visibleOrGone(
                    brandF.brandDiscount != null,
                    goneMode = View.INVISIBLE
                ) {
                    binding.tvBrandDiscount1.text = "${brandF.brandDiscount}%"
                }
                binding.ivSubscribe1.visibleOrInVisible(getSubState(brandF.brandId ?: "") == true)

                trackData(
                    page = SpmPageDef.BrandAllPage,
                    element = SpmElementDef.BrandCell,
                    showRatio = 0.1f,
                    params = trackMap(
                        brandAlltemBean.index,
                        brandF.name ?: "",
                        getSubState(brandF.brandId ?: "") == true
                    ),
                    businessSuffixID = businessSuffixId(brandF)
                )

                click {
                    //跳品牌二级页
                    navigationTo(RoutePage.Brand.BRAND_DETAIL) {
                        withString(RoutePage.Brand.ARG_BRAND_ID, "${brandF.brandId}")
                        withBoolean(
                            RoutePage.Brand.ARG_IS_DISCOUNT_BRAND,
                            brandF.brandDiscount != null
                        )
                    }
                }
            }
            binding.llBrand2.visibleOrGone(visible = it.size > 1, goneMode = View.INVISIBLE) {
                val brandAlltemBean = it[1]
                val brandF = brandAlltemBean.topBrandInfoVOF
                binding.ivBrandLogo2.load(brandF.logo, brandF.name)
                binding.tvBrandName2.text = brandF.name
                binding.llDiscount2.visibleOrGone(
                    brandF.brandDiscount != null,
                    goneMode = View.INVISIBLE
                ) {
                    binding.tvBrandDiscount2.text = "${brandF.brandDiscount}%"
                }
                binding.ivSubscribe2.visibleOrInVisible(getSubState(brandF.brandId ?: "") == true)
                trackData(
                    page = SpmPageDef.BrandAllPage,
                    element = SpmElementDef.BrandCell,
                    showRatio = 0.1f,
                    params = trackMap(
                        brandAlltemBean.index,
                        brandF.name ?: "",
                        getSubState(brandF.brandId ?: "") == true
                    ),
                    businessSuffixID = businessSuffixId(brandF)
                )
                click {
                    //跳品牌二级页
                    navigationTo(RoutePage.Brand.BRAND_DETAIL) {
                        withString(
                            RoutePage.Brand.ARG_BRAND_ID,
                            "${brandF.brandId}"
                        )
                        withBoolean(
                            RoutePage.Brand.ARG_IS_DISCOUNT_BRAND,
                            brandF.brandDiscount != null
                        )
                    }
                }
            }
            binding.llBrand3.visibleOrGone(visible = it.size > 2, goneMode = View.INVISIBLE) {
                val brandAlltemBean = it[2]
                val brandF = brandAlltemBean.topBrandInfoVOF
                binding.ivBrandLogo3.load(brandF.logo, brandF.name)
                binding.tvBrandName3.text = brandF.name
                binding.llDiscount3.visibleOrGone(
                    brandF.brandDiscount != null,
                    goneMode = View.INVISIBLE
                ) {
                    binding.tvBrandDiscount3.text = "${brandF.brandDiscount}%"
                }
                binding.ivSubscribe3.visibleOrInVisible(getSubState(brandF.brandId ?: "") == true)
                trackData(
                    page = SpmPageDef.BrandAllPage,
                    element = SpmElementDef.BrandCell,
                    showRatio = 0.1f,
                    params = trackMap(
                        brandAlltemBean.index,
                        brandF.name ?: "",
                        getSubState(brandF.brandId ?: "") == true
                    ),
                    businessSuffixID = businessSuffixId(brandF)
                )
                click {
                    //跳品牌二级页
                    navigationTo(RoutePage.Brand.BRAND_DETAIL) {
                        withString(
                            RoutePage.Brand.ARG_BRAND_ID,
                            "${brandF.brandId}"
                        )
                        withBoolean(
                            RoutePage.Brand.ARG_IS_DISCOUNT_BRAND,
                            brandF.brandDiscount != null
                        )
                    }
                }
            }
        }
    }

    private fun trackMap(position: Int, brandName: String, hasSub: Boolean) = mutableMapOf(
        "index" to (position.toString()),
        "brandName" to (brandName),
        "followStatus" to (if (hasSub) "1" else "0")
    )
}